# uncompyle6 version 2.11.5
# Python bytecode 2.7 (62211)
# Decompiled from: Python 2.7.9 (default, Sep 17 2016, 20:26:04) 
# [GCC 4.9.2]
# Embedded file name: D:\project\inleader_frist_client\print_client\core\printer.py
# Compiled at: 2013-12-29 04:03:31
_SUPPORT_CUPS = True
import time
import os
import commands
from setting import setting, Vfs
try:
    import cups
except ImportError:
    _SUPPORT_CUPS = False

class ShellLpr:

    def __init__(self):
        self.conn = cups.Connection()
        self.default = None
        printers = self.conn.getPrinters().items()
        if len(printers) != 0:
            self.default = printers[0][0]
        return

    def printFile(self, file):
        cmd = 'PPD=/etc/cups/ppd/%s.ppd /usr/lib/cups/filter/imagetoraster pi 0 test 1 "" %s | lp -d "%s"' % (
         self.default, file, self.default)
        result = commands.getoutput(cmd)
        setting.logger.debug(cmd)
        end_flag = 'request id is %s-' % self.default
        pos = result.find(end_flag)
        if pos != -1:
            pos += len(end_flag)
            return int(result[pos:].split(' ')[0].strip())
        setting.logger.error(result)
        return -1

    def isComplete(self, jobId):
        attr = self.conn.getJobAttributes(jobId)
        if attr['time-at-completed'] is not None:
            return True
        else:
            return False

    def cancelJob(self, jobId):
        self.conn.cancelJob(jobId)

    def progress(self, jobId):
        attr = self.conn.getJobAttributes(jobId)
        if attr['job-media-progress'] is not None:
            return attr['job-media-progress']
        else:
            return 0


class CupsPrinter:

    def __init__(self):
        self.conn = cups.Connection()
        self.default = None
        printers = self.conn.getPrinters().items()
        if len(printers) != 0:
            self.default = printers[0][0]
        return

    def printStatus(self):
        return self.default

    def printFile(self, file):
        return self.conn.printFile(self.default, file, file, {})

    def isComplete(self, jobId):
        attr = self.conn.getJobAttributes(jobId)
        if attr['time-at-completed'] is not None:
            return True
        else:
            return False

    def cancelJob(self, jobId):
        self.conn.cancelJob(jobId)

    def progress(self, jobId):
        attr = self.conn.getJobAttributes(jobId)
        if attr['job-media-progress'] is not None:
            return attr['job-media-progress']
        else:
            return 0


class EmptyPrinter:

    def __init__(self):
        pass

    def printFile(self, file):
        self.count = 0
        return 100

    def isComplete(self, jobId):
        if self.count >= 5:
            return True
        else:
            self.count += 1
            time.sleep(1)
            return False

    def progress(self, jobId):
        return 100 - 100 / self.count


DefaultPrinter = EmptyPrinter
if _SUPPORT_CUPS:
    DefaultPrinter = ShellLpr